﻿//-----------------------------------------------------------------------
// <copyright file="Clipboard.cs" company="In The Hand Ltd">
//     Copyright © 2013-14 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
#if __IOS__
using UIKit;
#else
using Windows.ApplicationModel.DataTransfer;
#endif

namespace InTheHand.ApplicationModel.DataTransfer
{
    /// <summary>
    /// Gets and sets information from the clipboard object.
    /// </summary>
    [System.CLSCompliant(false)]
    public static class Clipboard
    {
        /// <summary>
        /// Removes all data from the Clipboard.
        /// </summary>
        public static void Clear()
        {
#if __IOS__
            UIPasteboard.General.SetData(null, "kUTTypePlainText");
#else
            System.Windows.Clipboard.SetText("");
#endif
        }

        /// <summary>
        /// Sets the current content that is stored in the clipboard object.
        /// </summary>
        /// <param name="content">Contains the content of the clipboard.
        /// If NULL, the clipboard is emptied.</param>
        /// <remarks>For Windows Phone only plain-text and Uris are supported on the clipboard.</remarks>
        public static void SetContent(DataPackage content)
        {
            // default action is clear clipboard
            string text = "";

            if (content != null)
            {
                DataPackageView view = content.GetView();
                if (view.Contains(StandardDataFormats.Text))
                {

                    Task<string> t = Task<string>.Run<string>(async () => { return await view.GetTextAsync(); });
                    t.Wait();
#if __IOS__
                    UIPasteboard.General.String = t.Result;
                    return;
#else
                    text = t.Result;
#endif
                }
                else if (view.Contains(StandardDataFormats.WebLink))
                {
                    Task<Uri> t = Task<string>.Run<Uri>(async () => { return await view.GetWebLinkAsync(); });
                    t.Wait();
#if __IOS__
                    UIPasteboard.General.Url = Foundation.NSUrl.FromString(t.Result.ToString());
                    return;
#else
                    text = t.Result.ToString();
#endif
                    
                }
                else if (view.Contains(StandardDataFormats.ApplicationLink))
                {
                    Task<Uri> t = Task<string>.Run<Uri>(async () => { return await view.GetApplicationLinkAsync(); });
                    t.Wait();
#if __IOS__
                    UIPasteboard.General.Url = Foundation.NSUrl.FromString(t.Result.ToString());
                    return;
#else
                    text = t.Result.ToString();
#endif
                }
                else
                {
                    // do nothing for unsupported data type
                    return;
                }
            }
#if __IOS__
            else
            {
                UIPasteboard.General.String = "";
            }
#else
            System.Windows.Clipboard.SetText(text);
#endif
        }
    }

   
}

#if WINDOWS_PHONE_APP
namespace System.Windows
{
    internal static class Clipboard
    {
        /// <summary>
        /// For Windows XAML Phone Applications use a separate app to push items to the Clipboard
        /// </summary>
        /// <param name="text"></param>
        public static void SetText(string text)
        {
            global::Windows.System.LauncherOptions options = new global::Windows.System.LauncherOptions();
            options.PreferredApplicationDisplayName = "Clipboarder";
            options.PreferredApplicationPackageFamilyName = "InTheHandLtd.Clipboarder";
            options.DisplayApplicationPicker = false;
            global::Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format("clipboard:Set?Text={0}", Uri.EscapeDataString(text))), options);
        }
    }
}
#endif